package com.university.controller;

import com.university.config.MinioConfig;
import com.university.enums.ResultEnum;
import com.university.infrastructure.Result;
import com.university.service.MinioService;
import com.university.utility.FileTypeJudgeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @description: minio控制器
 **/
@Api(tags = {"文件上传、下载、预览接口"})
@RequestMapping("/minio")
@RestController
@Slf4j
public class MinioController extends BaseController {

    @Resource
    private MinioService minioService;
    @Resource
    private MinioConfig minioConfig;

    /**
     * 上传文件
     *
     * @param file       上传的文件
     * @param fileType   文件类型，1图片 2PDF 3文档 4视频 5种子 6音乐
     * @param bucketName bucketName
     * @return 是否成功
     */
    @ApiOperation("上传文件")
    @PostMapping("/uploadFile")
    public Result uploadFile(MultipartFile file, int fileType, String bucketName) {
        try {
            bucketName = StringUtils.isNotBlank(bucketName) ? bucketName : minioConfig.getBucketName();
            if (!minioService.bucketExists(bucketName)) {
                minioService.makeBucket(bucketName);
            }
            String contentType = file.getContentType();
            String fileName = file.getOriginalFilename();
            String objectName = new SimpleDateFormat("yyyy/MM/dd/").format(new Date()) + UUID.randomUUID().toString().replaceAll("-", "")
                    + fileName.substring(fileName.lastIndexOf("."));

            InputStream inputStream = file.getInputStream();
            //判断文件类型
            if (fileType != FileTypeJudgeUtils.isFileType(FileTypeJudgeUtils.getType(file.getInputStream()))) {
                throw new Exception("请上传" + fileType + "文件");
            }
            minioService.putObject(bucketName, objectName, inputStream, contentType);
            inputStream.close();
            return ok(objectName);
        } catch (Exception e) {
            log.error("上传文件错误:", e.getMessage());
            return fail(ResultEnum.FILE_UPLOAD_ERROR.getMessage());
        }
    }

    @ApiOperation("预览文件")
    @GetMapping("/preview")
    public Result preview(String fileName, String bucketName) {
        String result = minioService.presignedGetObject(bucketName, fileName);
        return ok(result);
    }

    /**
     * 下载文件
     *
     * @param fileName   文件名
     * @param bucketName bucketName
     */
    @ApiOperation("下载文件")
    @GetMapping("/download")
    public Result download(String fileName, String bucketName, HttpServletResponse response) {
        minioService.downloadFile(bucketName, fileName, response);
        return ok();
    }

    /**
     * 删除
     *
     * @param fileName   文件名
     * @param bucketName bucketName
     * @return 删除成功
     */
    @ApiOperation("删除文件")
    @DeleteMapping("/delete")
    public Result delete(String fileName, String bucketName) {
        boolean isRemove = minioService.removeObject(bucketName, fileName);
        return ok(isRemove);
    }
}
